#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

int gCnt = 123;

void test()
{
    int num = 20;
    string name("wangdao");
    int age = 100;

    //可以将外部局部变量分别进行捕获（值捕获与引用捕获可以同时使用）
    [num, &age, name](int value){
        ++value;
        cout << "value = " << value << endl;
        cout << "num = " << num << endl;
        ++age;
        cout << "age = " << age<< endl;
        cout << "name = " << name << endl;
        cout << "gCnt = " << gCnt << endl;
    }(200);

    cout << "age = " << age << endl;

    cout << endl << endl;
    //= 将外部所有的局部变量全部以值捕获
    [=](int value) {
        cout << "value = " << value << endl;
        cout << "num = " << num << endl;
        cout << "age = " << age<< endl;
        cout << "name = " << name << endl;
    }(200);

    cout << endl << endl;
    //= 将外部所有的局部变量全部以值捕获
    [&](int value) {
        ++value;
        ++age;
        num += 20;
        name = "wuhan";
        cout << "value = " << value << endl;
        cout << "num = " << num << endl;
        cout << "age = " << age<< endl;
        cout << "name = " << name << endl;
    }(200);


    cout << endl << endl;
    /* [&, age](int value){ */
    [=, &age](int value){
        ++value;
        cout << "value = " << value << endl;
        cout << "num = " << num << endl;
        /* ++age; */
        cout << "age = " << age<< endl;
        cout << "name = " << name << endl;
    }(200);
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

